<template>
	<view>
		<Topsearch ref='topsearch' :searchKeyWord="searchKeyWord" :showFocus="showFocus" :currentCateIndex="cateIndex" @currentCateClick='changeCateIndex'
		 @searchEvent="searchOut"></Topsearch>
		<view class="search-main-contain">
			<view class="history-search">
				<view class="history-search-head">
					<view class="history-search-title">
						历史搜索
					</view>
					<view @click="clearHistory" class="icon iconfont iconshanchu"></view>
				</view>
				<view class="history-search-contain">
					<block v-for="(item,index) in historySearch" :key="index">
						<view class="history-search-item" @click="hisSearch(item)">
							{{item}}
						</view>
					</block>
				</view>
			</view>
			<view class="hot-search-contain">
				<view class="hot-search-top">
					<view class="history-search-title">
						大家都在搜
					</view>
					<view class="icon iconfont iconre"></view>
				</view>
				<view class="history-search-contain">
					<block v-for="(item,index) in hotSearch" :key="index">
						<view class="history-search-item" @click="hisSearch(item)">
							{{item}}
						</view>
					</block>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import Topsearch from '@/components/top-search-tab.vue'
	import {
		saveHistoryWorld,
		loadHistory
	} from '@/utils/search.js'
	export default {
		data() {
			return {
				searchKeyWord: '',
				cateIndex: 0,
				showFocus:true,
				historySearch: [
					'大数据',
					'物联网',
					'人工智能',
					'电子信息'
				],
				hotSearch: [
					'大数据',
					'人工智能',
					'新能源',
					'新材料',
					'生物医药',
					'先进改造',
					'电子信息'
				]
			}
		},
		components: {
			Topsearch
		},
		onLoad() {
			this.showFocus = true;
		},
		onShow() {
			this.historySearch = loadHistory();
		},
		methods: {
			hisSearch(keyword) {
				let cateItem = this.cateIndex === 0 ? 'xm' : (this.cateIndex === 1 ? 'zj' : 'hd');
				const params = {
					keyWord: keyword,
					cateItem: cateItem,
					cateIndex: this.cateIndex
				};
				this.searchOut(params);
			},
			searchOut(data) {
				if (data.keyWord == 'undefined' || data.keyWord == '') {
					uni.showToast({
						title: '请输入搜索内容',
						duration: 1000,
						icon: "none"
					})
					return false;
				}
				saveHistoryWorld(data.keyWord);
				uni.navigateTo({
					url: '/pagesA/search-detail/search-detail?param=' + encodeURIComponent(JSON.stringify(data))
				})
				this.$refs.topsearch.clearSearchInput()
			},
			changeCateIndex(index) {
				this.cateIndex = index;
			},
			//删除历史搜索
			clearHistory() {
				this.historySearch = [];
				uni.removeStorageSync('historyWords');
			}
		}
	}
</script>

<style scoped>
	.searc-tab {
		width: 100%;
		height: 182upx;
		background-color: rgba(243, 243, 243, 1);
		padding: 14upx 39upx 25upx 30upx;
	}

	.search-area {
		position: relative;
		display: flex;
		align-items: center;
	}

	.search-input {
		width: 436upx;
		height: 72upx;
		margin-right: 24upx;
		border-radius: 36upx;
		background-color: #FFFFFF;
		padding: 0 66upx 0 92upx;
		font-size: 28upx;
		font-family: PingFangSC-Regular, PingFang SC;
		font-weight: 400;
		color: rgba(51, 51, 51, 1);
	}

	.search-input-text {
		font-size: 32upx;
		font-family: PingFangSC-Regular, PingFang SC;
		font-weight: 400;
		color: rgba(42, 125, 250, 1);
	}

	.search-icon {
		position: absolute;
		top: 15upx;
		left: 42upx;
		font-size: 28upx;
		font-family: PingFangSC-Regular, PingFang SC;
		font-weight: 400;
		color: rgba(187, 187, 187, 1);
	}

	.search-clear-icon {
		position: absolute;
		top: 0upx;
		right: 32upx;
		font-size: 28upx;
		height: 100%;
		/* border: 1px solid black; */
		font-family: PingFangSC-Regular, PingFang SC;
		font-weight: 400;
		color: rgba(187, 187, 187, 1);
		display: flex;
		align-items: center;
		z-index: 10;
	}

	.search-cate {
		display: flex;
		justify-content: center;
		align-items: center;
		margin-top: 26upx;
	}

	.search-cate-item-active {
		color: rgba(42, 125, 250, 1);
	}

	.search-cate-item {
		margin-right: 103upx;
		font-size: 32upx;
		font-family: PingFangSC-Regular, PingFang SC;
		font-weight: 400;
		color: rgba(155, 155, 155, 1);
	}

	.last-item {
		margin-right: 0upx;
	}

	.search-main-contain {
		width: 100%;
		margin-top: 20upx;

	}

	.search-main-top-contain {
		width: 100%;
		height: 30upx;
		background-color: rgba(243, 243, 243, 1);
	}

	.search-main-top-contain-in {
		width: 100%;
		height: 30upx;
		background-color: #FFFFFF;
		border-radius: 20upx 20upx 0 0;
	}

	.history-search {
		margin: 0upx 26upx 0upx 30upx;
	}

	.history-search-head {
		display: flex;
		justify-content: space-between;
	}

	.history-search-title {
		font-size: 30upx;
		font-family: PingFangSC-Medium, PingFang SC;
		font-weight: 500;
		color: rgba(51, 51, 51, 1);
	}

	.history-search-head>.icon {
		color: #BEBEBE
	}

	.history-search-contain {
		display: flex;
		flex-wrap: wrap;
		/* justify-content: space-between; */
		margin-top: 33upx;
	}

	.history-search-item {
		padding: 12upx 24upx;
		background: rgba(249, 250, 249, 1);
		border-radius: 8px;
		font-size: 26upx;
		margin: 12upx 0upx;
		font-family: PingFangSC-Regular, PingFang SC;
		font-weight: 400;
		color: rgba(74, 74, 74, 1);
		margin-right: 24upx;
	}

	.hot-search-contain {
		margin: 45upx 30upx 0upx 30upx;
		border-top: 1upx solid #F2F2F2;
		padding-top: 35upx;
	}

	.hot-search-top {
		display: flex;
	}

	.hot-search-top>.icon {
		color: #FE8037;
		font-size: 33upx;
		margin-left: 11upx;
	}
</style>
